<?php
/**
 * Created by Daniel X -- PhpStorm.  2020/7/24 15:30
 */
namespace App\Services;

use App\Models\Event;

class EventService
{
    protected $model;
    public function __construct(Event $model)
    {
        $this->model = $model;
    }

    /**
     * 获取 事件列表
     * @return mixed
     * @throws \Exception
     */
    public function getEventData()
    {
        $res = $this->model->get();
        $res = $res->toArray();
        $data = self::getTree($res);
        return $data;
    }

    public function getTree($data,$p_id=0)
    {
        $tree = [];
        foreach ($data as $k => $v){
            if($v['parent_id'] == $p_id){
                $v['parent_id'] = Self::getTree($data,$v['event_id']);
                $tree[] = $v;
            }
        }
        return $tree;
    }
}